/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package genetic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

/**
 *
 * @author skyr
 */
public class GeneralController {
    
    public static final int maxSolutionNumber = 10;
    private static int id = 0;
    
    public static int getNewId(){
        return id++;
    }

    public static Integer fitnessFunction(int maxI, int maxJ, Integer[][] costArray, Integer[][] solution) {
        Integer cost = 0;
        for (int i = 0; i < maxI; i++) {
            for (int j = 0; j < maxJ; j++) {
                cost = cost + (costArray[i][j] * solution[i][j]);
            }
        }
        return cost;
    }

    /**
     * phgh se ena sugkentroth
     */
    public static boolean restriction1(int maxI, int maxJ, Integer[][] solution) {
        int sum;
        for (int i = 0; i < maxI; i++) {
            sum = 0;
            for (int j = 0; j < maxJ; j++) {
                sum = sum + solution[i][j];
                if (sum > 1) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * xwritikothta kai fortio
     */
    public static boolean restriction2(int phges, int sygkentrotes, Integer[][] solution, Integer[] fortio, Integer[] xwritikothta) {
        int sunolikoFortio;
        for (int j = 0; j < sygkentrotes; j++) {
            sunolikoFortio = 0;
            for (int i = 0; i < phges; i++) {
                sunolikoFortio = sunolikoFortio + (solution[i][j] * fortio[i]);
            }
            if (sunolikoFortio > xwritikothta[j]) {
                return false;
            }
        }
        return true;
    }

    public static Collection<Solution> sortSolutions(Collection<Solution> solutions) {
        Collections.sort(new ArrayList<Solution>(solutions), new Comparator<Solution>() {

            @Override
            public int compare(Solution o1, Solution o2) {
                return (int) (o1.getCost() - o2.getCost());
            }
        });
        return solutions;
    }
}
